package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.tbank.ebank.pay.TBankMessage;

/**
 * 测试银行B2B后台银行回应
 * 
 * @TBankServerResController.java
 * @author WangHongFei
 */
@Controller
@Scope("prototype")
public class TBankB2BServerRes extends BankResBase {

	private static Log logger = LogFactory.getLog(TBankB2BServerRes.class);
    @Resource(name = "tbankConfig")
    private Map<String, String> tbankConfig;
    
    @RequestMapping(value = "/tbankB2bServerRes")
    protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
    	logger.error("--------------- TBankB2BServerRes bankback  ---------------");
        return super.execute(request, response);
    }
    
    @Override
    protected BaseMessage buildMessage(HttpServletRequest request) {
        TBankMessage message = new TBankMessage();
        // 银行码赋值
	    message.setBank(BankConstants.BANK_LOGO_TBANK);
        message.setOrderId(request.getParameter("orderNo"));
        message.setBankOrderId(message.getOrderId());
        message.setAmount(new BigDecimal(request.getParameter("orderAmount")));
        message.setRetCd(request.getParameter("retCd"));
        message.setRetMsg(request.getParameter("retMsg"));
        message.setSignMessage(request.getParameter("signData"));
		//银行业务产品01企业网银支付
        message.setBankBusProd(BisBankBusinessProd._01);
        return message;
    }

    @Override
    protected OrderInfo buildOrderInfo(HttpServletRequest request,Result result) {
    	String retCode = request.getParameter("retCd");
        OrderInfo orderInfo = new OrderInfo();
        if(retCode!=null && retCode.equals("S0200")){
            orderInfo.setOrderId(request.getParameter("orderNo"));
            orderInfo.setAmt(request.getParameter("orderAmount"));
            orderInfo.setNotifyNum("1");
            orderInfo.setPayStatus("00");
            orderInfo.setRetCode("00");
            orderInfo.setRetMsg(request.getParameter("resMsg"));
        }else{
            orderInfo.setOrderId(request.getParameter("orderNo"));
            orderInfo.setAmt(request.getParameter("orderAmount"));
            orderInfo.setNotifyNum("1");
            orderInfo.setPayStatus("01");
            orderInfo.setRetCode("01");
            orderInfo.setRetMsg(request.getParameter("resMsg"));
        }
        // 信用卡类型
        String bankCardType = tbankConfig.get("TBANK.BANKCARDTYPE");
        orderInfo.setCardType(bankCardType);
        if (StringUtils.equals("DebitCard", bankCardType)) {
            orderInfo.setCardType("DebitCard");
        } else if (StringUtils.equals("CreditCard", bankCardType)) {
            orderInfo.setCardType("CreditCard");
        }
        return orderInfo;
    }

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("orderNo"))
  				|| StringUtils.isEmpty(request.getParameter("orderAmount"))) {
		return false;
		}
		return true;
	}
}
